<?php
use \GatewayWorker\Lib\Gateway;
class PM_k
{
    /**
     * 用户申请解散房间
     * @info:注意，这里的解散，如果是开的人申请解散；则允许解散，如果不是开的人，是存在房间内的人申请；则让他滚蛋即可
     * @info：如果一个房间成功解散，那么这个房间全部数据，销毁，且恢复用户的钻石
     * @param $room_id
     * @param $user_id
     * @param $db_connect  数据标识
     */
    public static function __index( $room_id , $user_id , $db_connect ){
        //取出房间信息
        $room_info  = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        if( $room_info == false || $room_info == null || count( $room_info ) <= 0 ){
            return false;
        }
        //当前游戏是否已开始？已开始不允许解散
        if( $room_info['room_status'] != 0 ){
            return false;
        }
        //是否在位置上；当然这种不再位置上，按理说永远执行不到的
        $find = false;
        foreach ( $room_info as $key => $value ){
            if (strstr($key, 'room_player_') != false && $value != 0 ) {
                if( $value == $user_id ){
                    $find = true;
                }
            }
        }
        if( $find == false ){
            return false;
        }
        //是否为开房者解散(这个地方同样适用于代开)
        if( $user_id == $room_info['room_cid'] ){
            //给所有用户下发解散消息
            self::__Boss_JieSan( $room_info , $user_id , $db_connect );
            //删除整个room以及room对应的LOG信息
            PHZ_Log::__DeleteRoom_ALLLOG( $room_id , $db_connect );
            //删除整个room
            PHZ_Room::__deleteRoom( $room_id , $db_connect );
            //恢复用户的钻石(假如扣除了的话)
            if( $room_info['consume'] >= 1 ){
                PHZ_Member::__UserAdd_consume( $user_id , $room_info['consume'] , $db_connect );
            }
            //成功解散，更新房间缓存信息
            PHZ_Memcache::__set_RoomInfo_ToMemcache( $room_info , true );
        }else{//如果不是开房者解散；那么就是普通用户执行了退出操作；这个时候需要发送log，前端的人执行操作
            //对room进行清除操作
            $user_index = PHZ_User::__get_user_Room_Index( $room_info , $user_id );
            $db_connect->update('tt_room')->cols(array('room_player_' . $user_index => 0 ))->where('room_id=' . $room_id)->query();//踢掉这个傻逼
            //清除登录过的LOG日志
            PHZ_log::__Delete_JieSan_USERLOG( $room_id , $room_info['room_js'] , $user_id , $db_connect );
            //向当前存在房间内的用户发送固定消息(有用户退出)
            self::__User_JieSan( $room_info , $user_id , $db_connect );
        }
        //更新所有人的mem记录
        foreach ( $room_info as $key => $value ){
            if (strstr($key, 'room_player_') != false && $value != 0 ) {
                //更新心跳记录&mem记录
                PHZ_Card::__update_MAX_HeaderID( $room_info , $value , $db_connect );//更新用户的当前最大可执行ID
            }
        }
        //执行完毕
        return true;
    }

    /**
     * 房主要求解散
     * @param $room_info
     * @param $user_id
     * @param $db_connect
     */
    public static function __Boss_JieSan( $room_info , $user_id , $db_connect  ){
        foreach ( $room_info as $key => $value ){
            if (strstr($key, 'room_player_') != false && $value != 0 ) {
                if( $value != $user_id ){
                    $user_clientID = PHZ_Member::__getUserClientID( $value , $db_connect );
                    if( $user_clientID != "" && Gateway::isOnline( $user_clientID ) == 1 ){
                        $data = array();
                        $data['log_rid']     = $room_info['room_id'];
                        $data['log_jid']     = $room_info['room_js'];
                        $data['log_uid']     = (int)$user_id;//退出者的ID
                        $data['log_to_uid']  = $value;
                        $data['log_md']      = 20;
                        $data['log_data']    = json_encode( array() );
                        Gateway::sendToClient( $user_clientID , json_encode( $data ) ) ;
                    }
                }
            }
        }
    }

    /**
     * 发送普通用户退出的消息
     * @param $room_info
     * @param $user_id
     * @param $db_connect
     */
    public static function __User_JieSan( $room_info , $user_id , $db_connect ){
        foreach ( $room_info as $key => $value ){
            if (strstr($key, 'room_player_') != false && $value != 0 ) {
                if( $value != $user_id ){
                    $user_clientID = PHZ_Member::__getUserClientID( $value , $db_connect );
                    if( $user_clientID != "" && Gateway::isOnline( $user_clientID ) == 1 ){
                        $data = array();
                        $data['log_rid']     = $room_info['room_id'];
                        $data['log_jid']     = $room_info['room_js'];
                        $data['log_uid']     = (int)$user_id;//退出者的ID
                        $data['log_to_uid']  = $value;
                        $data['log_md']      = 19;
                        $data['log_data']    = json_encode( array() );
                        Gateway::sendToClient( $user_clientID , json_encode( $data ) ) ;
                    }
                }
            }
        }
    }

}